package com.andnnl.streamtest1;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by chenss on 2019/11/29.
 */
public class TestMaxMin {
    private static Random random = new Random();

    public static List<Integer> createList(int num) {
        List<Integer> list = new ArrayList<>();

        while (num > 0) {
            num--;
            list.add(random.nextInt(100));
        }
        return list;
    }

    public static void main(String[] args) {
        List<Integer> list = createList(10);
        System.out.println(list);
        Map<String, List<Integer>> map = list.stream().collect(Collectors.toMap(a -> "key"
                , (Integer i) -> Arrays.asList(i, i)
                , (List<Integer> l1, List<Integer> l2) -> Arrays.asList(
                        (l1.get(0) > l2.get(0) ? l1.get(0) : l2.get(0))
                        , l1.get(1) < l2.get(1) ? l1.get(1) : l2.get(1))
        ));
        System.out.println(map);

        MyComparator<Integer> comparator = new MyComparator();
        int max = list.stream().max(comparator).get();
        System.out.println(max);
        System.out.println(comparator.getMin());


    }

    static class MyComparator<T extends Integer> implements Comparator<T> {
        T min;

        @Override
        public int compare(T o1, T o2) {

            int i = o1.compareTo(o2);
            min = min == null && i < 0 ? o1 : o2;
            if (min.compareTo(i < 0 ? o1 : o2) > 0) {
                min = i < 0 ? o1 : o2;
            }
            return i;
        }

        public T getMin() {
            return min;
        }
    }

}
